import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.io.File;
import sun.audio.*;
import java.io.FileInputStream;
import javax.sound.sampled.*;
public class Windows extends JFrame{
	private ImageIcon[] comp = {
		new ImageIcon("Bust.jpg"), new ImageIcon("Credits.jpg"), new ImageIcon("Delete.jpg"), new ImageIcon("DSPane.jpg"),
		new ImageIcon("Head.jpg"), new ImageIcon("Hips.jpg"),new ImageIcon("Insert.jpg"),new ImageIcon("IPane.jpg"),
		new ImageIcon("Load.jpg"), new ImageIcon("Manual.jpg"),new ImageIcon("Name.jpg"), new ImageIcon("Name1.jpg"),
		new ImageIcon("Replace.jpg"), new ImageIcon("Save.jpg"), new ImageIcon("Search.jpg"), new ImageIcon("Waist.jpg"), 
		new ImageIcon("Up.jpg"), new ImageIcon("Down.jpg"), new ImageIcon("Image.jpg"), new ImageIcon("Cred.jpg")};
	private JButton insert, delete, search, replace, save, load, manual, credits, up, down;
	private JLabel head, ipane, dspane, name, name1, bust, waist, hips, disp, stat, image;
	private JTextField nameTF, name1TF, bustTF, waistTF, hipsTF, imageTF;
	private ImageIcon temp = new ImageIcon("Temp.jpg");
	private int lop = 1;
	public Windows(){
		super("Sexy Lister by ChanahC von Arbs");
		
		Container c = getContentPane();
		c.setLayout(null);
		while(lop==1){
			playStartupSound();
		}
		head = new JLabel(comp[4]);
		head.setSize(450, 100);
		head.setLocation(0,0);
		
		c.add(head);
		
		ipane = new JLabel(comp[7]);
		ipane.setSize(300, 75);
		ipane.setLocation(150, 100);
		c.add(ipane);
		name = new JLabel(comp[10]);
		name.setSize(100, 75);
		name.setLocation(150, 175);
		c.add(name);
		nameTF = new JTextField(30);
		nameTF.setSize(200, 75);
		nameTF.setLocation(250, 175);
		c.add(nameTF);
		bust = new JLabel(comp[0]);
		bust.setSize(100, 75);
		bust.setLocation(150, 250);
		c.add(bust);
		bustTF = new JTextField(30);
		bustTF.setSize(200, 75);
		bustTF.setLocation(250, 250);
		c.add(bustTF);
		waist = new JLabel(comp[15]);
		waist.setSize(100, 75);
		waist.setLocation(150, 325);
		c.add(waist);
		waistTF = new JTextField(30);
		waistTF.setSize(200, 75);
		waistTF.setLocation(250, 325);
		c.add(waistTF);
		hips = new JLabel(comp[5]);
		hips.setSize(100, 75);
		hips.setLocation(150, 400);
		c.add(hips);
		hipsTF = new JTextField(30);
		hipsTF.setSize(200, 75);
		hipsTF.setLocation(250, 400);
		c.add(hipsTF);
		dspane = new JLabel(comp[3]);
		dspane.setSize(300, 75);
		dspane.setLocation(150, 550);
		c.add(dspane);
		image = new JLabel(comp[18]);
		image.setSize(100, 75);
		image.setLocation(150, 475);
		c.add(image);
		imageTF = new JTextField(30);
		imageTF.setSize(200, 75);
		imageTF.setLocation(250, 475);
		c.add(imageTF);
		name1 = new JLabel(comp[11]);
		name1.setSize(100, 75);
		name1.setLocation(150, 625);
		c.add(name1);
		name1TF = new JTextField(30);
		name1TF.setSize(200, 75);
		name1TF.setLocation(250, 625);
		c.add(name1TF);
		final MyDatabase db = new MyDatabase();
		insert = new JButton(comp[6]);
		insert.setSize(150, 75);
		insert.setLocation(0, 100);
		insert.setCursor(new Cursor(Cursor.HAND_CURSOR));
		insert.setToolTipText("By clicking this button, you will be able to insert the Lady to the database. Take Note: Bawal maglista ng bakla");
		insert.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Node bago = new Node();
					bago.name = nameTF.getText();
					bago.bust = Double.parseDouble(bustTF.getText());
					bago.waist = Double.parseDouble(waistTF.getText());
					bago.hips = Double.parseDouble(hipsTF.getText());
					String imgname = "DBPics\\";
					imgname = imgname + imageTF.getText();
					bago.img = new ImageIcon(imgname);
					db.insert(bago);
				}
			}
			);
		c.add(insert);
		delete = new JButton(comp[2]);
		delete.setToolTipText("By clicking this button, you will delete the Lady which is equal to the Lady typed on the Deletion and Search Pane");
		delete.setSize(150, 75);
		delete.setLocation(0, 175);
		delete.setCursor(new Cursor(Cursor.HAND_CURSOR));
		delete.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Node bago = new Node();
					bago.name = name1TF.getText();
					boolean c = db.delete(bago);
				}
			}
			);
		c.add(delete);
		search = new JButton(comp[14]);
		search.setToolTipText("By clicking this button, the system will see if there is a Lady which is equal to the Lady on the DSPAne");
		search.setSize(150,75);
		search.setLocation(0, 250);
		search.setCursor(new Cursor(Cursor.HAND_CURSOR));
		search.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Node bago = new Node();
					bago.name = name1TF.getText();
					Node c = db.search(bago);
					JOptionPane.showMessageDialog(null, c, "Sexy Lister!!!", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			);
		c.add(search);
		replace = new JButton(comp[12]);
		replace.setToolTipText("By clicking this button, you will be able to delete the Lady and get it replaced by a new Lady");
		replace.setSize(150, 75);
		replace.setLocation(0, 325);
		replace.setCursor(new Cursor(Cursor.HAND_CURSOR));
		replace.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					Node bago = new Node();
					bago.name = nameTF.getText();
					bago.bust = Double.parseDouble(bustTF.getText());
					bago.waist = Double.parseDouble(waistTF.getText());
					bago.hips = Double.parseDouble(hipsTF.getText());
					String imgname = "DBPics\\";
					imgname = imgname + imageTF.getText();
					bago.img = new ImageIcon(imgname);
					Node luma = new Node();
					luma.name = name1TF.getText();
					db.replace(bago, luma);
				}
				}
				);
		c.add(replace);
		save = new JButton(comp[13]);
		save.setToolTipText("Saves the DB to a selected file");
		save.setSize(150, 75);
		save.setLocation(0, 400);
		save.setCursor(new Cursor(Cursor.HAND_CURSOR));
		save.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent a){
					JFileChooser f = new JFileChooser();
					int option = f.showOpenDialog(Windows.this);
					if (option == JFileChooser.APPROVE_OPTION){
						File file = new File(f.getSelectedFile().getName());
						try{
							boolean stf = db.saveToFile(file);
						}catch(FileNotFoundException e){
							JOptionPane.showMessageDialog(null, "Walang ganyang file!\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}catch(IOException e){
							JOptionPane.showMessageDialog(null, "I/O Error\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}catch(Exception e){
							JOptionPane.showMessageDialog(null, "Exception occured\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}
					}
				}
			}
			
			);
			c.add(save);
			load = new JButton(comp[8]);
			load.setToolTipText("Loads object from certain file");
			load.setSize(150, 75);
			load.setLocation(0, 475);
			load.setCursor(new Cursor(Cursor.HAND_CURSOR));
			load.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent a){
						JFileChooser f = new JFileChooser();
					int option = f.showOpenDialog(Windows.this);
					if (option == JFileChooser.APPROVE_OPTION){
						File file = new File(f.getSelectedFile().getName());
						try{
							boolean stf = db.loadFromFile(file);
						}catch(FileNotFoundException e){
							JOptionPane.showMessageDialog(null, "Walang ganyang file!\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}catch(IOException e){
							JOptionPane.showMessageDialog(null, "I/O Error\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}catch(Exception e){
							JOptionPane.showMessageDialog(null, "Exception occured\n", "Sexy Lister", JOptionPane.WARNING_MESSAGE);
						}
					}
				}
				}
				);
			c.add(load);
			manual = new JButton(comp[9]);
			manual.setToolTipText("Syempre, yan yung naglalaman ng Instruction!!!");
			manual.setCursor(new Cursor(Cursor.HAND_CURSOR));
			manual.setSize(150, 75);
			manual.setLocation(0, 550);
			manual.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent a){
						Manual m = new Manual();
					}
				}
				);
			c.add(manual);
			credits = new JButton(comp[1]);
			credits.setToolTipText("I-click mo na lang!!!!");
			credits.setCursor(new Cursor(Cursor.HAND_CURSOR));
			credits.setSize(150, 75);
			credits.setLocation(0, 550);
			credits.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent a){
						JFrame frame2 = new JFrame();
						JLabel label2 = new JLabel(comp[19]);
						label2.setLocation(0,0);
						label2.setSize(1000, 550);
						frame2.setContentPane(label2);
    					frame2.setSize(1020, 500);
    					frame2.setResizable(false);
    					frame2.setVisible(true);
					}
				}
				);
			c.add(credits);
			
			disp = new JLabel(temp);
			disp.setSize(400, 550);
			disp.setLocation(450, 0);
			c.add(disp);
			
			stat = new JLabel("Haven't browsed yet");
			stat.setSize(300, 150);
			stat.setLocation(500, 550);
			c.add(stat);
			
			up = new JButton(comp[16]);
			up.setSize(50, 150);
			up.setToolTipText("See previous");
			up.setCursor(new Cursor(Cursor.HAND_CURSOR));
			up.setLocation(450, 550);
			up.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent a){
						Node q = db.listback();
						String w = q.toString();
						disp.setIcon(q.img);
						stat.setText(w);
					}
				}
				);
			c.add(up);
			
			down = new JButton(comp[17]);
			down.setSize(50, 150);
			down.setToolTipText("See next");
			down.setCursor(new Cursor(Cursor.HAND_CURSOR));
			down.setLocation(800, 550);
			down.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent a){
						Node q = db.listIterator();
						String w = q.toString();
						disp.setIcon(q.img);
						stat.setText(w);
					}
				}
				);
			c.add(down);
			
			setSize(850, 700);
			setVisible(true);
			setResizable(false);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
	public static void main(String args[]){
		SwingUtilities.invokeLater(new Runnable() {
      	 public void run() {
      	   Windows x = new Windows();
     	  }
	    });
	}
	private void playStartupSound() {
    Runnable soundPlayer = new Runnable() {
       public void run() {
          try {
             // use one of the WAV of Windows installation
             File tadaSound = new File("c1.wav");     
             AudioInputStream audioInputStream = AudioSystem
                   .getAudioInputStream(new FileInputStream(tadaSound));
             AudioFormat audioFormat = audioInputStream
                   .getFormat();
             DataLine.Info dataLineInfo = new DataLine.Info(
                   Clip.class, audioFormat);
             Clip clip = (Clip) AudioSystem
                   .getLine(dataLineInfo);
             clip.open(audioInputStream);
             clip.start();
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    };
    Thread soundPlayingThread = new Thread(soundPlayer);
    soundPlayingThread.start();
 }
}